package com.zxjbyte.yiyi.framework.common.domain;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import com.zxjbyte.yiyi.framework.common.exception.ServiceException;

import java.io.*;
import java.util.Objects;

/**
 * 文件元数据
 * @Author zhangxingjia
 * @Date 2023/3/22 11:12
 * @Version: 1.0
 */
public class FileItem {

    // 文件名
    private String fileName;
    // MIME媒体类型
    private String mimeType;
    // 字节流内容
    private byte[] content;
    // 文件
    private File file;

    public FileItem(File file) {
        this.file = file;
    }

    public FileItem(String filePath) {
        this(new File(filePath));
    }

    public FileItem(String fileName, byte[] content) {
        this.fileName = fileName;
        this.content = content;
    }

    public FileItem(String fileName, InputStream input, boolean isClose) {
        this.fileName = fileName;
        this.content = IoUtil.readBytes(input, isClose);
    }

    public FileItem(String fileName, InputStream input) {
        this(fileName, input, true);
    }

    public FileItem(String fileName, byte[] content, String mimeType) {
        this(fileName, content);
        this.mimeType = mimeType;
    }

    public FileItem(String fileName, InputStream input, boolean isClose, String mimeType) {
        this(fileName, input, isClose);
        this.mimeType = mimeType;
    }

    public FileItem(String fileName, InputStream input, String mimeType) {
        this(fileName, input, true,  mimeType);
    }

    /**
     * 获取文件名称
     * @return
     */
    public String getFileName() {
        if (Objects.isNull(this.fileName) && Objects.nonNull(this.file) && this.file.exists()) {
            this.fileName = file.getName();
        }
        return this.fileName;
    }

    /**
     * 以字节数组的形式返回文件的内容
     * @return
     */
    public byte[] getBytes() {
        if(Objects.nonNull(this.content)){
            return this.content;
        } else if(FileUtil.isNotEmpty(this.file)){
            return FileUtil.readBytes(this.file);
        }
        throw new ServiceException("文件字节流或文件为空");
    }

    /**
     * 文件媒体类型
     * @return
     */
    public String getMimeType() {
        if(StrUtil.isNotEmpty(this.mimeType)){
            return this.mimeType;
        }
        String suffix = null;
        if (FileUtil.isNotEmpty(this.file)) {
            suffix = FileTypeUtil.getType(file);
        } else if (Objects.nonNull(this.content)) {
            if (StrUtil.isNotBlank(this.fileName)) {
                suffix = FileTypeUtil.getType(IoUtil.toStream(this.content), this.fileName);
            } else {
                suffix = FileTypeUtil.getType(IoUtil.toStream(this.content));
            }
        }
        return FileUtil.getMimeType("." + suffix);
    }

    /**
     * 文件媒体类型如果为空则返回指定的默认媒体类型
     *
     * @param defaultMimeType 默认的媒体类型
     * @return
     */
    public String getMimeTypeEmptyToDefault(String defaultMimeType) {
        String mimeType = this.getMimeType();
        if(StrUtil.isEmpty(mimeType)){
            return defaultMimeType;
        }
        return mimeType;
    }

    /**
     * 写道指定的输出流中
     * @param out
     * @throws IOException
     */
    public void writeFileContent(OutputStream out) throws IOException {
        if (Objects.isNull(out)){
            return;
        }
        if (Objects.nonNull(this.content)) {
            IoUtil.write(out, false, this.content);
        } else if (FileUtil.isNotEmpty(this.file)) {
            IoUtil.write(out, false, FileUtil.readBytes(file));
        } else {
            throw new IOException("文件字节流或文件为空");
        }
    }

    /**
     * 返回文件内容长度
     * @return
     */
    public long fileContentLength() {
        if (Objects.nonNull(this.content)) {
            return this.content.length;
        } else if (FileUtil.isNotEmpty(this.file)) {
            return this.file.length();
        } else {
            return 0;
        }
    }
}

